package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingListMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingListService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private OrderSettingListMapper orderSettingListMapper;

    @Override
    public Result order(Map map,Integer addressId) throws Exception {
        //1.检查是否能够预约
        String orderDate = (String) map.get("orderDate");   //获取日期
        OrderSetting orderSetting = orderMapper.findByOrderDate(orderDate);
        if (orderSetting == null) {
            //指定日期无法进行预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.检查是否约满了
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            //预约满了
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //3. 检查预约的用户是否重复预约 (当天同一个套餐预约)
        Member member = memberMapper.findByTelephone((String) map.get("telephone"));
        //用户是否存在
        if (member != null) {
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(DateUtils.parseString2Date(orderDate));
            order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
            List<Order> list = orderMapper.findByCondition(order);
            if (list != null && list.size() > 0) {
                //存在重复预约 返回失败
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //用户不存在 创建用户
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber((String) map.get("telephone"));
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberMapper.add(member);
        }
        //预约成功 保存到数据库
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String) map.get("orderType"));   //预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO); //预约状态
        order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
        orderMapper.add(order);   //添加预约信息
        //新增预约与预约地址关联关系
        Integer order_id = order.getId();
        //map1 存放order_id address_id
        Map map1 = new HashMap();
        map1.put("order_id", order_id);
        map1.put("address_id",addressId);
        //添加关联表信息
        orderSettingListMapper.addAddressAssociation(map1);

        //设置已预约人数+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingMapper.editReservations(orderSetting);
        //预约成功 发送短信
//        SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,(String) map.get("telephone"),"success");

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception{
        Map map = orderMapper.findById4Detail(id);
        if(map != null){
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
